<?php
namespace app\common\model;
use app\common\service\VoiceService;
use think\Db;
use think\Model;

/**
 * 技师语音播报配置
 * @package app\admin\model
 */
class EngineerVoice extends Model
{
    protected $name = 'qt_send_voice_msg';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 技师预约发送语音提示
     * @param $appointmentId [预约ID]
     * @param $type [类型 1 预约 2 取消预约]
     */
    public static function sendAppointmentVoice($appointmentId,$type){
        $appointment = Db::name('qt_room_appointment')->where(['id'=>$appointmentId])->find();
        if(!empty($appointment['engineer_id'])){
            //查询技师是否在房间
            $engineerData = Db::name('qt_order_queue')
                ->field('oq.room_id,r.name as room_id,u.work_number,r.name as room_name')
                ->alias('oq')
                ->leftJoin('qt_room r','r.id = oq.room_id')
                ->leftJoin('qt_users u','u.id = oq.user_id')
                ->where(['user_id'=>$appointment['engineer_id']])
                ->find();
            if($type == '1'){
                if($engineerData['room_id'] != null){
                    $text = '您有在'.$engineerData['room_name'].'房有新预约';
                }else{
                    $text = '您有新的预约需要处理';
                }
            }
            if($type == '2'){
                if($engineerData['room_id'] != null){
                    $text = $engineerData['work_number'].'号技师'.$engineerData['room_name'].'房取消预留';
                }else{
                    $text = $engineerData['work_number'].'号技师取消预留';
                }
            }
            $voiceResult = VoiceService::createVoice($text);
            if($voiceResult['code'] == 0){
                VoiceService::saveVoiceMsg($appointment['store_id'],$text,$voiceResult['data'],'7','3',$appointment['engineer_id']);
            }
        }
        return true;

    }




}
